<?php
class Admin_Model_Product extends Zend_Db_Table {

  protected $_name = 'products';
  protected $_primary = 'id';

  public function listItem($arrParam = null, $options = null) {
    $db = Zend_Registry::get('connectDb');
    // $db = Zend_Db::factory ( $adapter, $config );
    $paginator = $arrParam['paginator'];
    $ssFilter = $arrParam['ssFilter'];

    if ($options['task'] == 'product-list') {
      $select = $db->select()->from('products as p', array('*'))->joinLeft('product_category as pc', 'pc.id = p.cat_id', 'pc.name as category_name')->order('p.id DESC');

      /*
       * if (! empty($ssFilter['col']) && ! empty($ssFilter['order'])) {
       * $select->order($ssFilter['col'] . ' ' . $ssFilter['order']); }
       */

      if ($paginator['itemCountPerPage'] > 0) {
        $page = $paginator['currentPage'];
        $rowCount = $paginator['itemCountPerPage'];
        $select->limitPage($page, $rowCount);
      }

      if (!empty($ssFilter['keywords'])) {
        $keywords = '%' . $ssFilter['keywords'] . '%';
        $select->where('p.name LIKE ?', $keywords);
      }

      /*  if ($ssFilter['group_id'] > 0) {
         $select->where('u.group_id = ?', $ssFilter['group_id']);
       } */
      $result = $db->fetchAll($select);

    }
    return $result;
  }

  public function countItem($arrParam = null, $options = null) {
    $db = Zend_Registry::get('connectDb');
    // $db = Zend_Db::factory ( $adapter, $config );
    $ssFilter = $arrParam['ssFilter'];
    $select = $db->select()->from('products as p', array('COUNT(p.id) as totalItem'));

    /*
     * if (! empty($ssFilter['keywords'])) { $keywords = '%' .
     * $ssFilter['keywords'] . '%'; $select->where('u.user_name LIKE ?',
     * $keywords); } if ($ssFilter['group_id'] > 0) { $select->where('u.group_id
     * = ?', $ssFilter['group_id']); }
     */

    $result = $db->fetchOne($select);
    return $result;
  }

  public function saveItem($arrParam = null, $options = null) {
    if ($options['task'] == 'product-add') {
      $row = $this->fetchNew();
      $row->name = $arrParam['name'];
      $row->picture = $arrParam['picture'];
      $row->price = $arrParam['price'];
      $row->special = $arrParam['special'];
      $row->cat_id = $arrParam['cat_id'];
      $row->content = $arrParam['content'];
      $row->status = $arrParam['status'];
      $row->order = $arrParam['order'];
      $row->created = date("Y-m-d");
      $row->created_by = 1;
      $row->save();
    }

    if ($options['task'] == 'product-edit') {
      $where = 'id = ' . $arrParam['id'];
      $row = $this->fetchRow($where);
      $row->name = $arrParam['name'];
      $row->picture = $arrParam['picture'];
      $row->price = $arrParam['price'];
      $row->special = $arrParam['special'];
      $row->cat_id = $arrParam['cat_id'];
      $row->content = $arrParam['content'];
      $row->status = $arrParam['status'];
      $row->order = $arrParam['order'];
      $row->modified = date("Y-m-d");
      $row->modified_by = 1;
      $row->save();
    }
  }

  public function changeStatus($arrParam = null, $options = null) {
    $cid = $arrParam['cid'];
    if (count($cid) > 0) {
      if ($arrParam['type'] == 1) {
        $status = 1;
      } else {
        $status = 0;
      }
      $ids = implode(',', $cid);
      $data = array('status' => $status);
      $where = 'id IN (' . $ids . ')';
      $this->update($data, $where);
    }

    if ($arrParam['id'] > 0) {
      if ($arrParam['type'] == 1) {
        $status = 1;
      } else {
        $status = 0;
      }

      $data = array('status' => $status);
      $where = 'id = ' . $arrParam['id'];
      $this->update($data, $where);
    }
  }

  public function deleteItem($arrParam = null, $options = null) {
    if ($options['task'] == 'product-delete') {
      //----------LAY TEN HINH ANH user_avatar --------------
      /* $row = $this->getItem($arrParam, array('task' => 'delete'));
      //--------------XOA user_avatar -----------------------
      $upload_dir = UPLOADS_PATH . '/users';
      $upload = new Zendvn_File_Upload();
      $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
      $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
      $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);*/

      $where = ' id = ' . $arrParam['id'];
      $this->delete($where);
    }

    if ($options['task'] == 'product-multi-delete') {
      $cid = $arrParam['cid'];
      if (count($cid) > 0) {
        foreach ($cid as $key) {
          $arrParam['id'] = $key;
          //----------LAY TEN HINH ANH user_avatar --------------
          $row = $this->getItem($arrParam, array('task' => 'delete'));
          //--------------XOA user_avatar -----------------------
          /*$upload_dir = UPLOADS_PATH . '/users';
          $upload = new Zendvn_File_Upload();
          $upload->removeFile($upload_dir . '/orignal/' . $row['user_avatar']);
          $upload->removeFile($upload_dir . '/img100x100/' . $row['user_avatar']);
          $upload->removeFile($upload_dir . '/img450x450/' . $row['user_avatar']);*/
        }
        $ids = implode(',', $cid);
        $where = 'id IN (' . $ids . ')';
        $this->delete($where);
      }
    }
  }

  public function getItem($arrParam = null, $options = null) {

    if ($options['task'] == 'product-info' || $options['task'] == 'product-edit') {
      $db = Zend_Registry::get('connectDb');
      // $db = Zend_Db::factory($adapter,$config);
      $select = $db->select()->from('products as p')->joinLeft('product_category as pc', 'p.cat_id = pc.id', array('name as category_name'))->where('p.id = ?', $arrParam['id']);
      $result = $db->fetchRow($select);
    }
    if ($options['task'] == 'delete') {
      $where = 'id = ' . $arrParam['id'];
      $result = $this->fetchRow($where)->toArray();
    }
    return $result;
  }

  public function sortItem($arrParam = null, $options = null) {
    $cid = $arrParam['cid'];
    $order = $arrParam['order'];
    if (count($cid) > 0) {
      foreach ($cid as $key => $val) {
        $where = ' id = ' . $val;
        $data = array('order' => $order[$val]);
        $this->update($data, $where);
      }
    }
  }
}
